This notebook may be copied from docs/ and used as a trainer-tutor.
Download_Openslide_Data
Requires three lines of code:
1) import the function
2) declare the parameters
3) call the function with the parameters
from pychunklbl.toolkit import image_file_to_patches_directory_for_image_level
help(image_file_to_patches_directory_for_image_level)
run_parameters = {'wsi_filename': '../data/images/CMU-1-Small-Region.svs',
'output_dir': '../../mounted_dir/results',
'class_label': 'batch-name',
'patch_height': 224,
'patch_width': 224,
'patch_select_method': 'threshold_rgb2lab',
'thumbnail_divisor': 10,
'file_ext': '.jpg',
'threshold': 0,
'image_level': 0}
# display the parameters
for k, v in run_parameters.items():
print('%25s: %s'%(k, v))
import time
function_start_time = time.time()
image_file_to_patches_directory_for_image_level(run_parameters)
print('function running time: %0.3f'%(time.time() - function_start_time))
import os
from PIL import Image
for f_name in os.listdir(run_parameters['output_dir']):
im_file = os.path.join(run_parameters['output_dir'], f_name)
if os.path.isfile(im_file) == True:
print('\n',f_name)
display(Image.open(im_file))
"""
Import the toolkit function to read the template .yml file.
Read in the run_parameters and reset the output_dir location.
Note: "class_label" parameter is set to "test_label_name"
and will produce an reddish warning message when changed to "test-label-name"
"""
from pychunklbl.toolkit import get_run_parameters
parameter_data_dir = '../data/run_files'
# read the template file into a python dict
run_parameters = get_run_parameters(parameter_data_dir, 'wsi_file_to_patches_dir.yml')
# reset the output_dir location
run_parameters['output_dir'] = '../../run_dir/test_results'
# display the parameters
for k, v in run_parameters.items():
print('%25s: %s'%(k, v))
# Create the directory if it does not exist, else - check contents.
import os
if os.path.isdir(run_parameters['output_dir']) == False:
os.makedirs(run_parameters['output_dir'])
else:
out_dir_list = os.listdir(run_parameters['output_dir'])
print('\n\nOutput directory contents before calling the function %i entries'%len(out_dir_list))
(docs: requirements - Install OpenSlide, get slide image data)
"""
Display the original image in the rich openslide context
"""
import openslide
test_file_name = run_parameters['wsi_filename']
print('Opening Image file: \n\t{}\n'.format(test_file_name))
openslide_obj = openslide.OpenSlide(test_file_name)
print('openslide.OpenSlide(test_file_name) is type:', type(openslide_obj))
print('obj.level_count', openslide_obj.level_count)
print('obj.dimensions', openslide_obj.dimensions)
print('obj.level_dimensions', openslide_obj.level_dimensions)
print('obj.level_downsamples', openslide_obj.level_downsamples)
print('obj.properties are type', type(openslide_obj.properties))
print('\nobj.associated_images', openslide_obj.associated_images, type(openslide_obj.associated_images))
blind_region = openslide_obj.read_region((0, 0), 0, (200,200))
print('\nobj.read_region', openslide_obj.read_region((0, 0), 0, (200,200)))
scldwn = openslide_obj.level_downsamples[-1]
print('\nobj.get_best_level_for_downsample(%0.6f)'%(scldwn),
openslide_obj.get_best_level_for_downsample(scldwn))
thumbnail = openslide_obj.associated_images['thumbnail']
openslide_obj.close()
print('Slide thumbnail')
display(thumbnail)